<?php

	$t = stripslashes($_REQUEST['t']);
	$t = substr($t, 0, 255);

	$f = stripslashes($_REQUEST['f']);
	$s = stripslashes($_REQUEST['s']);
	$i = stripslashes($_REQUEST['i']);
	$sc = stripslashes($_REQUEST['sc']);
	

	$imgUrl = <<< EOF
http://farm{$f}.staticflickr.com/{$s}/{$i}_{$sc}_m.jpg
EOF;

	$img = getFlickrImage($imgUrl);
		
	$width = imagesx($img);
	$height = imagesy($img);
		
	$image_p = imagecreatetruecolor($width, $height);
	
	//single line
	$color = 'FFFFFF';
	$int = hexdec($color);
	$arr = array("red" => 0xFF & ($int >> 0x10),
		"green" => 0xFF & ($int >> 0x8),
		"blue" => 0xFF & $int);
	
	$clr = ImageColorAllocate($img, $arr["red"], $arr["green"], $arr["blue"]); 
	$black = imagecolorallocate($img, 0, 0, 0);

	$left = 20;
	$iwidth = $width-40;
	$top = $height - ($height/3);
	$fontsize = 12;
	$fontface = "./paths/Aquabase.ttf";

	imagefilledrectangle($img, $left, $top, $left+$iwidth, $top+($height/3)-10, imagecolorallocatealpha($img, 0, 0, 0, 100));
$left+=20;
	imagettftextbox($img, $fontsize, 0, $left-1, $top-1, $black, $fontface, $t, $iwidth);
	imagettftextbox($img, $fontsize, 0, $left+1, $top-1, $black, $fontface, $t, $iwidth);
	imagettftextbox($img, $fontsize, 0, $left-1, $top+1, $black, $fontface, $t, $iwidth);
	imagettftextbox($img, $fontsize, 0, $left+1, $top+1, $black, $fontface, $t, $iwidth);

	imagettftextbox($img, $fontsize, 0, $left, $top, $clr, $fontface, $t, $iwidth);


	Imagecopyresampled($image_p, $img, 0, 0, 0, 0, $width, $height, $width, $height);

	header("Content-type: image/jpeg");
	Imagejpeg($image_p); 

	ImageDestroy($img);
	ImageDestroy($image_p);	




function imagettftextbox(&$image, $size, $angle, $left, $top, $color, $font, $text, $max_width)
{
        $text_lines = explode("\n", $text); // Supports manual line breaks!
       
        $lines = array();
        $line_widths = array();
       
        $largest_line_height = 0;
       
        foreach($text_lines as $block)
        {
            $current_line = ''; // Reset current line
           
            $words = explode(' ', $block); // Split the text into an array of single words
           
            $first_word = TRUE;
           
            $last_width = 0;
           
            for($i = 0; $i < count($words); $i++)
            {
                $item = $words[$i];
                $dimensions = imagettfbbox($size, $angle, $font, $current_line . ($first_word ? '' : ' ') . $item);
                $line_width = $dimensions[2] - $dimensions[0];
                $line_height = $dimensions[1] - $dimensions[7];
               
                if($line_height > $largest_line_height) $largest_line_height = $line_height;
               
                if($line_width > $max_width && !$first_word)
                {
                    $lines[] = $current_line;
                   
                    $line_widths[] = $last_width ? $last_width : $line_width;
                                                         
                    $current_line = $item;
                }
                else
                {
                    $current_line .= ($first_word ? '' : ' ') . $item;
                }
               
                if($i == count($words) - 1)
                {
                    $lines[] = $current_line;
                   
                    $line_widths[] = $line_width;
                }
               
                $last_width = $line_width;
                   
                $first_word = FALSE;
            }
           
            if($current_line)
            {
                $current_line = $item;
            }
        }
       
        $i = 0;
        foreach($lines as $line)
        {
            
            imagettftext($image, $size, $angle, $left, $top + $largest_line_height + ($largest_line_height * $i), $color, $font, $line);
            $i++;
        }
       
        return $largest_line_height * count($lines);
}
function getFlickrImage($url)
{

	$ch = curl_init($url);
	
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
	curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1); 
	curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; U; FreeBSD i386; en-US; rv:1.2a) Gecko/20021021");
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
	curl_setopt($ch, CURLOPT_TIMEOUT, 60);

	$ret = curl_exec($ch);
	$errno = @curl_errno($ch);
	$error = @curl_error($ch);
	curl_close($ch);
	
	return imagecreatefromstring($ret);
}
?> 
 
